<?php

require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';

class content_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		$objContent = new Models_Content();
		$objContentCategory = new Models_ContentCategory();
		
		/**
		 * Get category
		 */
		$categoryGid = $this->_getParam('cid',false);
		if (false == $categoryGid) {
			$this->_redirectToNotFoundPage();
		}
		
		$numRowPerPage = Nine_Registry::getConfig("defaultNumberRowPerPage");
		$currentPage = $this->_getParam("page",1);
		
		/**
		 * Get latest news
		 */
		$latestNews = $objContent->getLatestContentByCategory( $categoryGid );
		if (null == $latestNews) {
			$this->_redirectToNotFoundPage();
		}
		
		$cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?' => $latestNews['content_category_gid']))->toArray());
		if (null == $cat) {
			$this->_redirectToNotFoundPage();
		}
		
		$latestNews['category_name'] = $cat['name'];
		$latestNews['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($latestNews['title'])),30);
        $latestNews['full_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($latestNews['full_text'])), 145);
        $latestNews['url'] = Nine_Route::_("content/index/detail/id/{$latestNews['content_gid']}");
		$tmp = explode('||', $latestNews['images']);
		$latestNews['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 239);
		
		/**
		 * Get all contents
		 */
		$condition = array(
					'exclude_content_gids'	=>	$latestNews['content_gid']
					);
		
		
		$allContents = $objContent->getAllEnabledContentByCategory($categoryGid, $condition, array('sorting ASC','content_id DESC'),
                                                   $numRowPerPage,($currentPage - 1) * $numRowPerPage
                                                   );
		/**
		 * Modified all contents
		 */
		foreach($allContents as &$content){
			$content['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($content['title'])),30);
        	$content['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($content['intro_text'])), 145);
        	$content['url'] = Nine_Route::_("content/index/detail/id/{$content['content_gid']}");
        	$tmp = explode('||', $content['images']);
			$content['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 92, 78);
		}
        unset($content);                                           

        $allContents = Nine_Function::arrayChunkFixed($allContents, 2);
        
		/**
         * Count all contents
         */
        $count = count($objContent->getAllEnabledContentByCategory($categoryGid,$condition));
        
        /**
         * Assign to view
         */
        
		$this->setPagination($numRowPerPage, $currentPage, $count);
		$this->view->latestNews = $latestNews;
        $this->view->allContents = $allContents;
        /**
         * Get current category
         */
        $cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?' => $categoryGid))->toArray());
        $this->view->headTitle($cat['name']);
        $this->view->menuId = "news";
	}
	
	public function detailAction()
	{
		$objContent = new Models_Content();
		$objContentCategory = new Models_ContentCategory();
		
		$gid = $this->_getParam('id',false);
		if ( false == $gid) {
			$this->_redirectToNotFoundPage();
		}
		
		$news = $objContent->getContentByGid($gid)->toArray();
		
		if (null == $news) {
			$this->_redirectToNotFoundPage();
		}
		$this->view->headTitle($news['title']);
		$this->view->menuId = "home";
		
		/**
		 * Set template
		 */
		$cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?' => $news['content_category_gid']))->toArray());
		$templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
		$templatePath .= '/detail.' . Nine_Constant::VIEW_SUFFIX;
		
		
        $this->view->news = $news;
        
        
        /**
		 * Render this template
		 */
		$this->view->html = $this->view->render( $templatePath );
        
	}
	
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
}